//
//  UserDefaultsManager.swift
//  AIGirl
//
//  Created by Rae on 2024/1/17.
//

import Foundation

class UserDefaultsManager {
    
    // 定义键
    enum Key: String {
        case uid
        case searchHistory
        case versionUpdate
        case saveMode
        case activeAlert
        case chatAlert
        case integralAlert
        case gameAlert
        case giftAlert
        case signAlert
        case personalAlert
        case isRemember
        case phone
        case password
        case searchInfoHistory
        case browseHistory
        case picForumPost
        case videoForumPost
        case forumSearchHistory
        case squareSearchHistory
        case dynamicSearchHistory
        // 添加其他键...
    }
    
    // 保存字符串值
    class func setString(_ value: String, forKey key: Key) {
        UserDefaults.standard.set(value, forKey: key.rawValue)
        UserDefaults.standard.synchronize()
    }
    
    // 获取字符串值
    class func getString(forKey key: Key) -> String? {
        return UserDefaults.standard.string(forKey: key.rawValue)
    }
    
    // 保存布尔值
    class func setBool(_ value: Bool, forKey key: Key) {
        UserDefaults.standard.set(value, forKey: key.rawValue)
        UserDefaults.standard.synchronize()
    }
    
    // 获取布尔值
    class func getBool(forKey key: Key) -> Bool {
        return UserDefaults.standard.bool(forKey: key.rawValue)
    }
    
    // 保存Int
    class func setInt(_ value: Int, forKey key: Key) {
        UserDefaults.standard.set(value, forKey: key.rawValue)
        UserDefaults.standard.synchronize()
    }
    
    // 获取Int
    class func getInt(forKey key: Key) -> Int {
        return UserDefaults.standard.integer(forKey: key.rawValue)
    }
    
    // 保存字典
    class func setDict(_ value: [String: Any], forKey key: Key) {
        UserDefaults.standard.set(value, forKey: key.rawValue)
        UserDefaults.standard.synchronize()
    }
    
    // 获取字典
    class func getDict(forKey key: Key) -> [String: Any]? {
        return UserDefaults.standard.dictionary(forKey: key.rawValue)
    }
    
    // 保存对象数组
    class func setModelsData<T: Codable>(_ models: [T], forKey key: Key) {
        let defaults = UserDefaults.standard
        let encoder = JSONEncoder()
        do {
            let data = try encoder.encode(models) // 编码为 Data
            defaults.set(data, forKey: key.rawValue)       // 保存到 UserDefaults
            print("\(key) saved successfully!")
        } catch {
            print("Failed to save \(key): \(error)")
        }
    }
    
    // 获取对象数组
    class func getModelsData<T: Codable>(forKey key: Key) -> [T]? {
        let defaults = UserDefaults.standard
        let decoder = JSONDecoder()
        
        if let data = defaults.data(forKey: key.rawValue) { // 从 UserDefaults 获取 Data
            do {
                let models = try decoder.decode([T].self, from: data) // 解码为对象数组
                print("\(key) loaded successfully!")
                return models
            } catch {
                print("Failed to load \(key): \(error)")
            }
        }
        
        return nil
    }
    
    //remove
    class func removeForKey( forKey key: Key) {
        UserDefaults.standard.removeObject(forKey: key.rawValue)
        UserDefaults.standard.synchronize()
    }
    
    //clear
    class func clearUserDefaults() {

        let defaults = UserDefaults.standard
        
        defaults.removeObject(forKey: "uid")
        defaults.removeObject(forKey: "searchHistory")
        defaults.removeObject(forKey: "picForumPost")
        defaults.removeObject(forKey: "videoForumPost")
        
        defaults.synchronize()
    }
}

